const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const reload = browserSync.reload;
const getTask = require('./gulp/util.js')


gulp.task('clean', getTask('clean'));
gulp.task('inquire', getTask('inquire'));
gulp.task('sprites', getTask('sprites'));
gulp.task('libraryOutput', getTask('libraryOutput'));
gulp.task('html', getTask('html'));
gulp.task('scripts', getTask('scripts'));
gulp.task('css', getTask('css'));
gulp.task('sass', getTask('sass'));
gulp.task('less', getTask('less'));
gulp.task('img', getTask('img'));
gulp.task('public', getTask('public'));
gulp.task('config', getTask('config'));
gulp.task('staticSuffix', getTask('staticSuffix'));
gulp.task('zip', getTask('zip'));
gulp.task('server', getTask('server'));

// 默认任务
gulp.task(
	'default',
	gulp.series('inquire', 'clean', 'sprites', 'libraryOutput',
		gulp.parallel('html', 'css', 'sass', 'less', 'scripts', 'img'),
		gulp.parallel('config', 'public'),
		'staticSuffix',
		gulp.parallel('zip', 'server', function (event) {
			watchs(event)
		})
	));

gulp.task(
	'dev',
	gulp.series('clean', 'sprites', 'libraryOutput',
		gulp.parallel('html', 'css', 'sass', 'less', 'scripts', 'img'),
		gulp.parallel('config', 'public'),
		'staticSuffix',
		gulp.parallel('server', function (event) {
			watchs(event)
		})
	));

function watchs(done) {
	gulp.watch('src/static/images/sprite/*/*.png', gulp.series('sprites', 'watchPublic'));
	gulp.watch('src/static/library/**', gulp.series('libraryOutput', 'watchPublic'));
	gulp.watch('src/pages/**/*.html', gulp.series('html', 'watchPublic'));
	gulp.watch('src/static/style/**/*.css', gulp.series('css', 'watchPublic'));
	gulp.watch('src/static/style/**/*.sass', gulp.series('sass', 'watchPublic'));
	gulp.watch('src/static/style/**/*.less', gulp.series('less', 'watchPublic'));
	gulp.watch(['src/static/scripts/**/*.js', '!src/static/scripts/public/**/*.js'], gulp.series('scripts', 'watchPublic'));
	gulp.watch(['src/static/images/**/*.{svg,jpeg,png,jpg,gif}', '!src/static/images/sprite/*/*.{png,jpg,gif}'], gulp.series('img', 'watchPublic'));
	gulp.watch('src/static/scripts/public/**/*.js', gulp.series('public', 'watchPublic'));
	gulp.watch('src/static/config/**/*.js', gulp.series('config', 'watchPublic'));
	done();
}
gulp.task('watchPublic', gulp.series('staticSuffix'));